/**
 * Created by IntelliJ IDEA.
 * User: dinesh
 * Date: Apr 14, 2012
 * Time: 4:14:41 PM
 * To change this template use File | Settings | File Templates.
 */
/**
 * Created by IntelliJ IDEA.
 * User: dinesh
 * Date: Feb 09, 2013
 * Time: 8:29:44 PM
 * To change this template use File | Settings | File Templates.
 */
/***************************/

  //global vars
	var form = $("#frmData");
	var name = $("#fullName");
	var nameInfo = $("#fullNameInfo");
    var initName = $("#initName");
	var initNameInfo = $("#initNameInfo");
	var email = $("#email");
	var emailInfo = $("#emailInfo");
	var pass1 = $("#pass1");
	var pass1Info = $("#pass1Info");
	var pass2 = $("#pass2");
	var pass2Info = $("#pass2Info");
    var nic= $("#nic");
	var birthday = $("#birthday");
    var gender = $("#gender");
    var _birthday;


$(document).ready(function(){

	email.blur(validateEmail);
	pass1.blur(validatePass1);
	pass2.blur(validatePass2);
	pass1.keyup(validatePass1);
	pass2.keyup(validatePass2);

	form.submit(function(){
		if( validateEmail() & validatePass1() & validatePass2() )
			return true
		else
			return false;
	});
    disableForm("frmData");
} );


function validateEmail(){ 		
		var a = $("#email").val();
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		//if it's valid email
		if(filter.test(a)){
			email.removeClass("errorField");
			emailInfo.text("");
			emailInfo.removeClass("errorField");
			return true;
		}
		//if it's NOT valid
		else{
			email.addClass("errorField");
			emailInfo.text("Type a valid e-mail please ");
			emailInfo.addClass("errorField");
			return false;
		}
	}

function validatePass1(){
		var a = $("#password1");
		var b = $("#password2");

		//it's NOT valid
		if(pass1.val().length <5 ){
			pass1.addClass("errorField");
			pass1Info.text("At least 5 characters: letters, numbers and '_'");
			pass1Info.addClass("errorField");
			return false;
		}
		//it's valid
		else{
			pass1.removeClass("errorField");
			pass1Info.text("");
			pass1Info.removeClass("errorField");
			validatePass2();
			return true;
		}
	}
	function validatePass2(){
		var a = $("#password1");
		var b = $("#password2");
		//are NOT valid
		if( pass1.val() != pass2.val() ){
			pass2.addClass("errorField");
			pass2Info.text("Passwords doesn't match!");
			pass2Info.addClass("errorField");
			return false;
		}
		//are valid
		else{
			pass2.removeClass("errorField");
			pass2Info.text("");
			pass2Info.removeClass("errorField");
			return true;
		}
	}

function edit(){
    enableForm("frmData");
    enableElement("btnSave");
    disableElement("btnEdit");
    isEdit=true;
}


function changePassword(){
         document.getElementById('changePassword').style.display ="block";
        disableElement("btnChangPass");
}
function closePassword(){
         document.getElementById('changePassword').style.display ="none";
        enableElement("btnChangPass");
}
function save(){
    if(validateEmail() ) {
        var params = serializeForm("frmData") ;
        ajaxPost("?user/edit", params, function(data) {

			var response =JSON.parse(data);

			if (response.status==false) {
				return;
			}
          //  Alert("Message is: "+response.message + "type is :"+response.type);
            $("#messagePanel").removeClass();
            $("#messagePanel").addClass(response.type);
            $("#messagePanel").html(response.message );
		});
    }

		else
			return false;

}

  function resetPassword(){
    if(validatePass1() & validatePass2() ) {
        var params = serializeForm("frmDataPassword") ;
        ajaxPost("?user/password", params, function(data) {

			var response =JSON.parse(data);

			if (response.status==false) {
				return;
			}
             //Alert("Message is: "+response.message + "type is :"+response.type);
            $("#messagePanel").removeClass();
            $("#messagePanel").addClass(response.type);
            $("#messagePanel").html(response.message );
            closePassword();
            //document.getElementById("messagePanel").innerHTML="Message is: "+response.message + "type is :"+response.type;
		});
    }

		else
			return false;

}






function closePage(){
    window.location.href="?home";
}

function onLoad(){
    _birthday = new dhtmlXCalendarObject(["birthday"]);
    _birthday.hideTime();     
     enforcePrivileges();
    disableElement("btnSave");
}
//window.onload=doOnLoad;
onLoad();